<?php


namespace app\service;
use app\model\Result;
use think\facade\Db;

/**
 * 用户
 */
class UserService
{
    /**
     * 用户手机号密码登录
     */
    public static function passwordLogon($param) {
        $field = '*';

        $res =  Db::name('user')->field($field)->where('phone',$param['phone'])->where('status', 1)->find();
        return $res;
    }

    /**
     * 用户注册
     */
    public static function register($param) {

        $field = 'password';

        $res = Db::name('user')->field($field)->where('phone',$param['phone'])->find();
        if (!empty($res)) {
            return Result::index(100,'手机号已注册','');
        }

        $chars = md5(uniqid(mt_rand(), true));
        $param['uuid'] = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );

        $id = Db::name('user')->insertGetId($param);

        if (empty($id)) {
            return Result::index(100,'注册失败','');
        }

        return Result::index(200,'注册成功','');
    }

    /**
     * 更新用户信息
     */
    public static function userUpdate($param) {

        $res = Db::name('user')->where('uuid','=',$param['uuid'])->update([
            'name' => $param['name'],
            'address' => $param['address'],
            'password' => $param['password'],
            'phone' => $param['phone'],
            'sex' => $param['sex'],
            'head_img' => $param['head_img'],
            'autograph' => $param['autograph'],
            'synopsis' => $param['synopsis']
        ]);

        return Result::index(200,'更新成功',$res);

    }

    /**
     * 获取用户信息
     */
    public static function queryUserByUuid($param) {

        $field = '*';

        $love_num = Db::name('love')->field($field)->where('user_uuid',$param['uuid'])->where('status',1)->count();
        Db::name('user')->where('uuid',$param['uuid'])->update([
            'love_num' => (int)$love_num
        ]);

        $res = Db::name('user')->field($field)->where('uuid',$param['uuid'])->find();
        if(empty($res)){
            return Result::index(100,'查无此人',$res);
        }
        return Result::index(200,'获取信息成功',$res);

    }
}
